package com.foreveross.crawl.web.controller.monitor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.quartz.Scheduler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.foreveross.GlobalMapOperatorBeta1;
import com.foreveross.crawl.application.task.ITaskOperateApplication;
import com.foreveross.crawl.common.application.ITaskModelEntityApplication;
import com.foreveross.crawl.dto.TaskModelDto;
import com.foreveross.crawl.dto.TaskStatisticsDto;
import com.foreveross.crawl.vo.TaskStatisticsVO;
import com.foreveross.crawl.web.controller.auth.BaseController;
import com.foreveross.ehcache.CacheUtil;
import com.foreveross.util.SpliteJson;

@Controller
@RequestMapping("/monitor/Task")
public class TaskController extends BaseController {

	@Inject
	private ITaskOperateApplication iTaskOperateApplication;
	@Inject
	private ITaskModelEntityApplication iTaskModelEntityApplication;
	
	@ResponseBody
	@RequestMapping("/findTaskByGroup")
	public Map<String, Object> findTaskByGroup(Long groupId, Long start, Long limit){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		List<TaskModelDto> dtoList= GlobalMapOperatorBeta1.getTaskByGroup(groupId);
		dataMap.put("Rows", dtoList);
		dataMap.put("start", start);
		dataMap.put("limit", limit);
		dataMap.put("Total", dtoList.size());
		return dataMap;
	}
	
	@ResponseBody
	@RequestMapping("/findColums")
	public Map<String, Object> findColums(Long groupId){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		List<TaskModelDto> dtoList= GlobalMapOperatorBeta1.getTaskByGroup(groupId);
		if(dtoList == null){
			return null;
		}
		dataMap.put("colums", SpliteJson.packagingColumsForMatadata(dtoList.get(0)));
		return dataMap;
	}
	
	@ResponseBody
	@RequestMapping("/findGroup")
	public Map<String, Object> findGroup(){
		return GlobalMapOperatorBeta1.getCache("channelGoup");
	}
	
	@ResponseBody
	@RequestMapping("/findTaskByChannel")
	public Map<String, Object> findTaskByChannel(Long channelId, Long page, Long pageSize, String status){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		List<TaskModelDto> taskList = new ArrayList<TaskModelDto>();
		if(channelId == null){
			taskList.addAll(GlobalMapOperatorBeta1.getAllTask(status));
		} else {
			taskList.addAll(GlobalMapOperatorBeta1.getTaskByCondtion(channelId, status));
		}
		taskList.addAll(this.iTaskModelEntityApplication.getTaskModelByChannelIdAndState(channelId, status));
		
		int totalSize = taskList == null ? 0 : taskList.size();
		int pageRecord = 0;
		List<TaskModelDto> retList = new ArrayList<TaskModelDto>();
		
		if(totalSize <= pageSize){//首页
			retList = taskList;
		} else {
			pageRecord = (int) ((page - 1) * pageSize);
			for(int i = pageRecord; i < totalSize && i < pageRecord + pageSize; i++){
				retList.add(taskList.get(i));
			}
		}
		//按任务开始时间降序排列
//		Collections.sort(retList, new Comparator<TaskModelDto>(){
//			public int compare(TaskModelDto f1, TaskModelDto f2){
//				return f2.getTaskStartDate().compareTo(f1.getTaskStartDate());
//			}
//		});
		dataMap.put("data", retList);
		dataMap.put("resultCount", totalSize);
		return dataMap;
	}

	@ResponseBody
	@RequestMapping("/operateTask")
	public Map<String, Object> operateTask(@RequestBody List<TaskModelDto> taskDtos){
		
		Map<String, Object> dataMap = new HashMap<String, Object>();
//		Scheduler scheduler = CrawlContext.getInstance().getScheduler();
		
		String msg = "success";
		try {
			Scheduler scheduler = GlobalMapOperatorBeta1.getSchedulerByTaskId(taskDtos.get(0).getId());
			
			iTaskOperateApplication.updateTaskStatusOfBatch(taskDtos, scheduler);
		} catch (Exception e) {
			msg = "fail";
		}
		dataMap.put("msg", msg);
		return dataMap;
	}
	
	/**  
	 * @Description: 获取异常信息
	 * @param taskId
	 * @return
	 * @author luofangyi
	 * @date 2014-7-10 下午8:53:21 
	 */ 
	@ResponseBody
	@RequestMapping("/obtainException")
	public Map<String, Object> obtainException(String taskId){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("data", CacheUtil.getInstance().getObjectFromCache(taskId));
		return dataMap;
	}
	
	@ResponseBody
	@RequestMapping("/obtainTaskBill")
	public Map<String, Object> obtainTaskBill(Long page, Long pageSize){
		Map<String, Object> dataMap = new HashMap<String, Object>();
		int pageRecord = 0;
		
		List<TaskStatisticsDto> retList = new ArrayList<TaskStatisticsDto>();
		List<TaskStatisticsVO> taskStatisticsVOList= this.iTaskModelEntityApplication.statisticsTask();
		List<TaskStatisticsDto> dtoListFromDB = TaskStatisticsVO.getObtainTaskBillBeta2(taskStatisticsVOList);
		
		//统计缓存中的任务信息
//		List<TaskStatisticsDto> dtoList = TaskStatisticsVO.getObtainTaskBillBeta1();
		List<TaskStatisticsDto> dtoList = TaskStatisticsVO.getObtainTaskBillBeta1(dtoListFromDB);
		int totalSize = dtoList != null ? dtoList.size() : 0;
		if(totalSize <= pageSize){//首页
			retList = dtoList;
		} else {
			pageRecord = (int) ((page - 1) * pageSize);
			for(int i = pageRecord; i < totalSize && i < pageRecord + pageSize; i++){
				retList.add(dtoList.get(i));
			}
		}
		Collections.sort(retList, new Comparator<TaskStatisticsDto>(){
			public int compare(TaskStatisticsDto f1, TaskStatisticsDto f2){
				return f1.getChannelId().compareTo(f2.getChannelId());
			}
		});
		dataMap.put("data", retList);
		dataMap.put("resultCount", totalSize);
		return dataMap;
	}
	
}
